<template>
  <div class="login-container">
    <el-card class="box-card">
      <h2 class="title">问卷管理系统</h2>
      <el-form
        :model="ruleForm"
        status-icon
        ref="ruleForm"
        label-position="left"
        label-width="70px"
        class="login-form"
      >
        <el-form-item label="用户名" prop="username">
          <el-input v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="ruleForm.password"
            autocomplete="off"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <div class="confirm">
          <router-link to="/confirm">忘记密码</router-link>
        </div>
      </el-form>
      <el-space wrap  alignment="start" :size="15">
      <div class="btnGroup">
        <el-space wrap  alignment="start" :size="30">
        <el-button type="primary" @click="submitForm">登录</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <router-link to="/register">
          <el-button type="success">注册</el-button>
        </router-link>
      </el-space>
      </div>
      <div>
        <div class="Qloginbtn">
        <el-button type="warning" @click="FastLogin">游客登录</el-button>
      </div>
      </div>
    </el-space>
    </el-card>
  </div>
</template>

<script>
import axios from 'axios';
import request from '../../utils/request';
export default {
  data() {
    return {
      ruleForm: {
        username: "",
        password: "",
      },
    };
  },
  methods: {
    async submitForm() {
      const response = await request.post('login', this.ruleForm).then(res => {
        console.log(res);
        if (res.code === "200") {
          const responseData = res.data;
          localStorage.clear();
          sessionStorage.clear();
          localStorage.setItem("token", responseData.token);
          localStorage.setItem("role", responseData.role);
          localStorage.setItem("userid", responseData.id);
          this.$router.push('/Home');
          this.$message.success('登录成功');
          
          router.beforeEach((to, from, next) => {
          const isAuthenticated = checkUserAuthentication();
          if (to.meta.requiresAuth && !isAuthenticated) {
          next('/');
  } else {
    next(); // 确保继续进行路由跳转
  }
});
        } else {
          this.$message.error('登录失败');
          console.log("前端输入的数据：", this.ruleForm)
        }
      }).catch(err => {
        console.log(err);
        //this.$message.error('网络请求失败，请稍后再尝试');
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    async FastLogin(){
      const response = await request.post('quickLogin', null).then(res => {
        console.log(res);
        if (res.code === "200") {
          const responseData = res.data;
          localStorage.clear();
          localStorage.setItem("token", responseData.token);
          localStorage.setItem("role", responseData.role);
          localStorage.setItem("userid", responseData.id);
          this.$router.push('/Home');
          this.$message.success('快捷登录成功');
        } else {
          this.$message.error('快捷登录失败');
          console.log("前端输入的数据：", this.ruleForm)
        }
      }).catch(err => {
        console.log(err);
        this.$message.error('网络请求失败，请稍后再尝试');
      });
    },
  },
};
</script>

<style scoped>
.login-container {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  background-color: #f0f2f5;
  background-image: url('/src/assets/loginbg.jpg'); 
  background-size: cover; 
  background-position: center; 
}

.box-card {
  width: 400px;
  padding: 20px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  background-color: rgba(255, 255, 255, 0.8);
}

.title {
  text-align: center;
  font-size: 24px;
  color: #333;
  margin-bottom: 20px;
}

.login-form {
  margin: 0 auto;
  width: 100%;
}

.confirm {
  text-align: right;
  margin-top: -10px;
  margin-bottom: 20px;
}

.btnGroup {
  display: flex;
  justify-content: space-around;
  margin-bottom: 10px;
}

.el-button {
  width: 100px;
}

.Qloginbtn{
  display: flex;
  justify-content: space-around;
  margin-bottom: 10px;
}
</style>
